import {
    View,
    Text,
    StyleSheet,
    Image,
    Animated,
    Easing,
    ViewStyle,
    RefreshControl,
    ActivityIndicator
} from 'react-native'
import React, {Component} from 'react'
import {screenWidth} from "../../style";
import Toast, {DURATION} from 'react-native-easy-toast'
import HUDProgress from "../native/HUDProgress";


const maxWidth = screenWidth / 2.2;
export default class CustomToast {

    isShowing = false;

    componentDidMount() {
    }

    delayDismiss(callback?: () => void,timeout:number=800) {
        setTimeout(() => {
            callback && callback();
            this.hideToast();
        }, timeout);
    }

    async showSuccess(text: string="操作成功!", duration: number = 500, callback?: () => void) {
        this.isShowing &&await this.hideToast();
        this.isShowing = true;

        HUDProgress.showSuccessWithStatus(text);
        this.delayDismiss(callback);
        return;

    }

    async showError(text: string, duration: number = 500, callback?: () => void) {
        this.isShowing &&await this.hideToast();
        this.isShowing = true;

        HUDProgress.showErrorWithStatus(text);
        this.delayDismiss(callback);
    }

    async showLoading(text: string = "加载中...") {
        this.isShowing &&await this.hideToast();
        this.isShowing = true;
        HUDProgress.showWithStatus(text);
        this.delayDismiss(null,2000);
    }

    hideToast() {
        return new Promise(resolve => {
            this.isShowing = false;
            HUDProgress.dismiss(()=>{
                resolve();
            });
        })

    }

    hideLoading() {
        this.hideToast();
    }


}

const CustomToastStyle = StyleSheet.create({
    container: {}
});


// // v1
// import {View, Text, StyleSheet, Image, Animated, Easing, ViewStyle} from 'react-native'
// import React, {Component} from 'react'
// import {padding, rectWithSize, screenHeight, default as styles, border, roundWithSize} from "../../style";
// import Toast, {DURATION} from 'react-native-easy-toast'
// import IconFont from 'react-native-vector-icons/Ionicons'
//
//
// interface CustomToastProps {
//
// }
//
// interface CustomToastState {
//     rotate: Animated.Value
// }
//
// export default class CustomToast extends Component<CustomToastProps, CustomToastState> {
//
//     state = {
//         rotate: new Animated.Value(0)
//     };
//
//     constructor(props: CustomToastProps) {
//         super(props);
//     }
//
//     componentDidMount() {
//         // Animated.loop(Animated.timing(this.state.rotate, {
//         //     toValue: 360,
//         //     duration: 800,
//         //     easing: Easing.linear,
//         //     useNativeDriver: true
//         // })).start();
//     }
//
//     showSuccess(text: string, duration?: number, callback?: () => void) {
//         const toast: Toast = this.refs["toast"];
//         toast.show(<View style={[padding(5, 20), styles.centered]}>
//             <IconFont name={"ios-checkmark-circle"} size={40} color={"#fff"}/>
//             <Text style={{fontSize: 14, color: "#fff", marginTop: 6}}>{text}</Text>
//         </View>, duration, callback);
//     }
//
//     showError(text: string, duration?: number, callback?: () => void) {
//         const toast: Toast = this.refs["toast"];
//         toast.show(<View style={[padding(5, 20), styles.centered]}>
//             <IconFont name={"ios-close-circle"} size={40} color={"#fff"}/>
//             <Text style={{fontSize: 14, color: "#fff", marginTop: 6}}>{text}</Text>
//         </View>, duration, callback);
//     }
//
//     showLoading(text: string) {
//         // const toast: Toast = this.refs["toast"];
//         //
//         // toast.show(<View style={[padding(5, 20), styles.centered]}>
//         //         <FastImage source={loadingImage} style={rectWithSize(40)}/>
//         //     <Text style={{fontSize: 14, color: "#fff", marginTop: 10}}>{text}</Text>
//         // </View>, DURATION.FOREVER);
//     }
//
//     hideLoading(duration: number = 0) {
//         // const toast: Toast = this.refs["toast"];
//         // toast.close(duration);
//     }
//
//     render() {
//         return (<Toast fadeOutDuration={100} ref="toast" position='top'
//                        style={{backgroundColor: "rgba(0,0,0,.86)", borderRadius: 12}}
//                        positionValue={screenHeight / 2 - 100}/>)
//     }
// }
//
// const CustomToastStyle = StyleSheet.create({
//     container: {}
// });
//

